<?php
namespace app\[MODULE]\controller[FLEX];
use think\facade\Db;
use think\facade\Cache;
use think\facade\Request;
use app\[MODULE]\model[FLEX]\[UTABLE] as [UTABLE]Model;
use services\JsonService;
use services\UtilService as Util;
use think\facade\Route as Url;
use services\FormBuilder as Form;
use app\admin\controller\AuthController;

class [CONTROLLER] extends AuthController{
    

    /**
     * 首页
     */
    public function index(){
        if (Request::isAjax()) {
            $where = Util::getMore([
                ['page', 1],
                ['limit', 20]
            ]);
            return JsonService::successlayui([UTABLE]Model::getList($where));
        }
        return $this->fetch();

    }
    

    /**
     * 编辑模板消息
     * @param $id
     * @return mixed|\think\response\Json|void
     */
    public function form()
    {
       
        if($id = Request::post('id/d')){
            $detail = [UTABLE]Model::get($id);
            if (!$detail) return JsonService::fail('数据不存在!');
        }
      
           $f = [];
           [ITEM]
          $form = Form::make_post_form('', $f, Url::buildUrl('save', array('id' => $id)), 2);
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');
    }

     /**
     * 表单提交
     *
     * @return json
     */
    public function save(){
        $post_data = Request::post();
        if($post_data['id']){
            $res = [UTABLE]Model::update($postData);
        }else{
            $res = [UTABLE]Model::set($postData);
        }
        if($res){
             return JsonService::successful('保存成功');
        }else{
             return JsonService::fail('保存失败');
        }
    }
    
    /**
     * 快速编辑
     *
     * @return json
     */
    public function set_value($field = '', $id = '', $value = '')
    {
        $field == '' || $id == '' || $value == '' && JsonService::fail('缺少参数');
        if ([UTABLE]Model::where(['id' => $id])->update([$field => $value])) {
            return JsonService::successful('保存成功');
        } else {
            return JsonService::fail('保存失败');
        }

    }

        /**
     * @name: 删除
     * @Author: Sam
     * @Date: 2020-05-21 16:37:42
     * @msg:
     * @param {type}
     * @return:
     */
    public function delete()
    {
        $id = Request::param('id/a'); //转化数组
        !$id && JsonService::fail('缺少参数');
        if ([UTABLE]Model::del($id)) {
            return JsonService::successful('删除成功');
        } else {
            return JsonService::fail('删除失败');
        }

    }

    /**
     * 新增编辑
     */
    public function add(){
        $act     = Request::param('act');
        $id      = Request::param('id');
        if($act == 'edit' && $id){
            $info = Db::name('[TABLE]')->where('id',$id)->find();
            if($info){
                $this->assign('info',$info);
            }
        }
        return $this->fetch();
    }

    /**
     * 会员提交
     */
    public function  Handle(){
        if(Request::isAjax()){
            $data               = Request::post();
            $validate           = new [CONTROLLER]validate();
            $[TABLE]            = new [TABLE]Model();
            switch ($data['act']) {
                case 'add':
                    $result             = $validate->check($data);
                    if(true != $result){
                        $this->error($validate->getError());
                    }
                    $res = $[TABLE]->allowField(true)->save($data);
                    break;
                case 'edit':
                    $result             = $validate->check($data);
                        if(true != $result){
                            $this->error($validate->getError());
                        }
                    $res = $[TABLE]->allowField(true)->save($data,['id'=>$data['id']]);
                break;
                default:
                case 'del':
                    $res = $[TABLE]::destroy($data['id']);
                break;
                    break;
            }
            if($res){
                $this->success('操作成功！');
            }else{
                $this->error('操作失败');
            }   
        }else{
            $this->error('非法提交!');
        }
    }
}